using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using UniversityWebsite.DAL;

namespace TestSystem.DAL
{
    public abstract class DataAccess
    {
        protected const int NTEXT = int.MaxValue;

        public static string ConnectionString
        {
            get
            {
                string _connString = string.Empty;
                if (ConfigurationManager.ConnectionStrings[Global.CONNECTION_STRING_KEY] != null)
                {
                    _connString = ConfigurationManager.ConnectionStrings[Global.CONNECTION_STRING_KEY].ConnectionString;
                }
                return _connString;
            }
        }

        public static DbTransaction GetTransaction()
        {
            return GetTransaction(string.Empty);
        }

        public static DbTransaction GetTransaction(string transactionName)
        {
            SqlConnection cn = new SqlConnection(ConnectionString);
            cn.ConnectionString = ConnectionString;
            cn.Open();
            if (!string.IsNullOrEmpty(transactionName))
            {
                return cn.BeginTransaction(transactionName);
            }
            else
            {
                return cn.BeginTransaction();
            }
        }

        public static object ToDBValue<T>(T value) where T : IComparable
        {
            if (value == null || value.CompareTo(default(T)) == 0)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }

        public static T FromDBValue<T>(object value)
        {
            if (value == DBNull.Value || value == null)
            {
                return default(T);
            }
            else
            {
                return (T)value;
            }
        }
        
        public static int? IntFromDBValue(object value)
        {
            if (value == DBNull.Value || value == null)
                return null;
            else
                return (int?) value;
        }

        public static object DateTimeToDBValue(DateTime value)
        {
            if (value == System.Data.SqlTypes.SqlDateTime.MinValue.Value)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }

        public static DateTime DBValueToDateTime(object value)
        {
            if (value == DBNull.Value || value == null)
            {
                return System.Data.SqlTypes.SqlDateTime.MinValue.Value;
            }
            else
            {
                return (DateTime)value;
            }
        }

        public static bool FieldExists(IDataReader rdr, string fieldName)
        {
            bool res = false;
            for (int i = 0; i < rdr.FieldCount; i++)
            {
                if (rdr.GetName(i) == fieldName)
                {
                    res = true;
                    break;
                }
            }
            return res;
        }
    }
}
